from flask import Flask, request
import QQ_utils as api

app = Flask(__name__)


@app.route('/', methods=["POST"])
def Listen():

    req = request.get_json()
    post_type = req['post_type']
    # 通知上报
    if post_type == 'notice':
        notice_type = req['notice_type']    
        match notice_type:
            # 离线文件
            case "offline_file":
                user_id = req['user_id']
                file = req['file']
                file_name = file['name']
                url = file['url']
                size = file['size']
                
                user_info = api.getStrangerInfo(user_id)
                user_name = user_info['nickname']
                print(f"{user_name} 发来大小为{size}, 文件名为{file_name}的文件,下载链接为{url}")
    if post_type == 'message':
        message_type = req['message_type']  
        user_id = req['user_id']
        user_info = api.getStrangerInfo(user_id)
        user_name = user_info['nickname']
        raw_msg = req['raw_message']
        match message_type:
            # 私聊
            case 'private':
                raw_msg = req['raw_message']
                print(f"{user_name} 发来消息:\n{raw_msg}")
            case 'group':
                g_id = req['group_id']
                group_name = api.getGroupInfo(g_id)['group_name']
                print(f'{group_name} 中 {user_name} 发来消息:\n{raw_msg}')
    return 'ok'

if __name__ == '__main__':
    app.run(host = "127.0.0.1", port = 8000, debug=True)

